<?php
// +----------------------------------------------------------------------
// | Tplay [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://tplay.pengyichen.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 听雨 < 389625819@qq.com >
// +----------------------------------------------------------------------


namespace app\admin\model;

use app\api\controller\Common;
use app\admin\controller\Goods as GoodsCtl;
use think\Db;
use \think\Model;
use \think\Cookie;
class Goods extends Model
{

    public function admin()
    {
        //关联角色表
        return $this->belongsTo('Admin');
    }


    //主要城市
    public function city()
    {
        $city= model('City')->where('groupid','neq',0)->where('deleted','0')->select();
        //关联城市表
        return $city;
    }

    public function team($city='',$typeid=8)
    {
        if(!empty($typeid)){
            $where['typeid']=$typeid;
        }
        $where['deleted']=0;
        $where['groupid']=$city;
        $team= model('Poi')->where($where)->select();
        //关联服务商表
        return $team;
    }

    public function classify()
    {
        $classify= model('Classify')->where('classify_status','1')->select();
        //关联分类表
        return $classify;
    }


    public function tag()
    {
        $tag= model('Tag')->where('tag_type','2')->where('tag_status','NEQ','2')->select();
        //关联角色表
        return $tag;
    }

    public function poisee($id='')
    {
        $poi= model('Poi')->where('id',$id)->select();

        return $poi;
    }
    public function tripsee($id='')
    {
        $trip= model('Trip')->where('id',$id)->select();

        return $trip;
    }
    //POI
    public function poi($city='',$classify='')
    {
        $poi= model('Poi')->where('groupid',$city)->where('typeid',$classify)->where('deleted','NEQ','1')->select();

        return $poi;
    }

    public function assoc_unique($arr, $key) {
        $tmp_arr = array();
        foreach ($arr as $k => $v) {
            if (in_array($v[$key], $tmp_arr)) {//搜索$v[$key]是否在$tmp_arr数组中存在，若存在返回true
                unset($arr[$k]);
            } else {
                $tmp_arr[] = $v[$key];
            }
        }
        sort($arr); //sort函数对数组进行排序
        return $arr;
    }

    public function trip($city='')
    {
        $active=Db::name('active')->where('cityid',$city)->field('groupid')->select();

        $active=$this->assoc_unique($active,'groupid');

        $tripid= Array();
        $trip= Array();
        if($active){
            foreach ($active as $key=>$value){
                $schedule=Db::name('schedule')->where('id',$value['groupid'])->field('groupid')->select();
                if($schedule){
                    array_push($tripid,$schedule);
                }
            }

            $tripid=$this->assoc_unique($tripid,'0');
        }
        if($tripid){
            foreach ($tripid as $key=>$value){
                $tripmsg=Db::name('trip')->where('id',$value[0]['groupid'])->select();
                if($tripmsg){
                    array_push($trip,$tripmsg);
                }
            }
        }
        foreach ($trip as $key=>$value){
            $trip[$key]=$value[0];
        }

        return $trip;
    }
    //关联项目 产品
    public function product($city='')
    {
        $active=Db::name('pactive')->where('cityid',$city)->field('groupid')->select();

        $active=$this->assoc_unique($active,'groupid');

        $tripid= Array();
        $trip= Array();
        if($active){
            foreach ($active as $key=>$value){
                $schedule=Db::name('pschedule')->where('id',$value['groupid'])->field('groupid')->select();
                if($schedule){
                    array_push($tripid,$schedule);
                }
            }

            $tripid=$this->assoc_unique($tripid,'0');
        }
        if($tripid){
            foreach ($tripid as $key=>$value){
                $tripmsg=Db::name('product')->where('id',$value[0]['groupid'])->select();
                if($tripmsg){
                    array_push($trip,$tripmsg);
                }
            }
        }
        foreach ($trip as $key=>$value){
            $trip[$key]=$value[0];
        }

        return $trip;
    }


    public function edit($d=''){
        $s='';
        if ($d!=''){

            $d['updatetime']=time();
            $adminid = Cookie::get('admin');
            $d['admin_id']=$adminid;
            $aa= $this->where('goods_id',$d['goods_id'])->update($d);
            if ($aa==true){
                $s=true;
                return $s;
            }else{
                $s=false;
                return $s;
            }
        }else{
            $s=false;
            return $s;
        }
        return $s ;
    }



    //商品上下架
    public function shelves($req = []){
        if(empty($req['goods_status']) || empty($req['id'])) return false;
        //下架
        if($req['goods_status']=='1'){
            $upd['goods_status'] = 2;
        //上架
        }elseif($req['goods_status']=='2'){
            $upd['goods_status'] = 1;
            $upd['updatetime'] = time();
        }else{
            return false;
        }
        //更新上下架状态
        $res = db('goods')->where('goods_id',$req['id'])->update($upd);
        //上架通知到微信群
        $admin_id = Cookie::get('admin');
        if($res && $req['goods_status']=='2'){
            //(new Common())->sendUpGoodsMsgToWx($req['id'],$admin_id);
            (new GoodsCtl())->addUpdateGoodsNoticeLog($req['id'],[],1);//操作员修改订单上架状态 提醒OTA
        }
        return $res;
    }

    function personDayMaxAndMinPrice($goods_id,$starttime,$endtime)
    {
        $list = db("personday")->field("goods_id,person_price,specs_id")->where(["goods_id"=>$goods_id,"time"=>['between',[$starttime,$endtime]]])->select();
        if(empty($list)){
            return false;
        }else{
            $specsList = array_column($list,"specs_id");
            if(empty($specsList)){
                return false;
            }else{
                $specsList = array_unique($specsList);
                $spec_where['specs_id'] = ["in",$specsList];
                $spec_where['specs_status'] = 1;
                $specsData = db("specs")->where($spec_where)->column("specs_id");
                if(empty($specsData)){
                    return false;
                }else{
                    $min = $max = $list[0]['person_price'];
                    foreach ($list as $key=>$value){
                        if(in_array($value['specs_id'],$specsData)){
                            $min = $min < $value['person_price']?$min:$value['person_price'];
                            $max = $max > $value['person_price']?$max:$value['person_price'];
                        }
                    }

                    return ["min"=>$min,"max"=>$max];
                }
            }
        }
    }



}
